package com.gitee.neuray.security.modules.param.service.impl;

import com.gitee.neuray.security.common.annotation.DataFilter;
import com.gitee.neuray.security.common.utils.*;
import com.gitee.neuray.security.modules.param.entity.EngineParamEntity;
import com.gitee.neuray.security.modules.param.entity.ParamItemEntity;
import com.gitee.neuray.security.modules.param.service.EngineParamService;
import com.gitee.neuray.security.modules.param.service.ParamItemService;
import com.gitee.neuray.security.modules.sys.entity.SysUserEntity;
import com.gitee.neuray.security.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gitee.neuray.security.modules.param.dao.EngineItemDao;
import com.gitee.neuray.security.modules.param.entity.EngineItemEntity;
import com.gitee.neuray.security.modules.param.service.EngineManageService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("engineItemService")
public class EngineManageServiceImpl extends ServiceImpl<EngineItemDao, EngineItemEntity> implements EngineManageService {

    @Resource
    private EngineParamService engineParamService;

    @Resource
    private EngineItemDao engineItemDao;

    @Resource
    private ParamItemService paramItemService;

    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
		params.put("flag",1);
        params.put("pageNum",(Integer.parseInt(params.get("page").toString())-1)*Integer.parseInt(params.get("limit").toString()));
        params.put("pageSize",Integer.parseInt(params.get("limit").toString()));
        List<EngineItemEntity> list = engineItemDao.queryListByPage(params);
        params.put("flag",null);
        int count = engineItemDao.queryListByPage(params).size();
        PageUtils pageUtils = new PageUtils(list,count,Integer.parseInt(params.get("limit").toString()),Integer.parseInt(params.get("page").toString()));
        return pageUtils;    }

    @Override
    public PageUtils queryEngine(EngineItemEntity engineItemEntity) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEngine(EngineItemEntity engineItemEntity) {
        List<Long> params = engineItemEntity.getParamIdList();
        String pkID = UUIDUtils.generateShortUuid();
        //1关联表
        List<EngineParamEntity> engineParamEntities = new ArrayList<>();
        List<String> tableItem = new ArrayList<>();
        params.stream().forEach((param) -> {
            EngineParamEntity engineParamEntity = new EngineParamEntity();
            engineParamEntity.setParamId(param);
            engineParamEntity.setEngineId(pkID);
            EntityUtils.setCreatAndUpdatInfo(engineParamEntity);
            engineParamEntities.add(engineParamEntity);
            tableItem.add("param_" + param);
        } );
        engineParamService.saveBatch(engineParamEntities);
        //2建表
        //生成表名
        String tableUU = UUIDUtils.generateShortUuid();
        String tableName = "engine_" + tableUU;
        //3主表
        engineItemEntity.setTableName(tableName);
        engineItemEntity.setId(pkID);
        EntityUtils.setCreatAndUpdatInfo(engineItemEntity);
        this.save(engineItemEntity);
        engineItemDao.createTable(tableItem,tableName);
    }

    @Override
    public void updateEngine(EngineItemEntity engineItemEntity) {
        //删除关联表
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("engine_id",engineItemEntity.getId());
        engineParamService.remove(queryWrapper);
        //插入关联表
        List<EngineParamEntity> engineParamEntities = new ArrayList<>();
        List<String> tableItem = new ArrayList<>();
        engineItemEntity.getParamIdList().stream().forEach((param) -> {
            EngineParamEntity engineParamEntity = new EngineParamEntity();
            engineParamEntity.setParamId(param);
            engineParamEntity.setEngineId(engineItemEntity.getId());
            EntityUtils.setCreatAndUpdatInfo(engineParamEntity);
            engineParamEntities.add(engineParamEntity);
            tableItem.add("param_" + param);
        } );
        engineParamService.saveBatch(engineParamEntities);
        //删除生成表
        if(engineItemEntity.getTableName() != null){
            engineItemDao.dropTable(engineItemEntity.getTableName());
        }
        //生成表名
        String tableUU = UUIDUtils.generateShortUuid();
        String tableName = "engine_" + tableUU;

        //修改主表
        engineItemEntity.setTableName(tableName);
        EntityUtils.setUpdatedInfo(engineItemEntity);
        this.updateById(engineItemEntity);
        //执行生成表
        engineItemDao.createTable(tableItem,tableName);
    }

    @Override
    public void deleteEngine(List<String> ids,List<String> list) {
        this.removeByIds(ids);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("engine_id",ids);
        engineParamService.remove(queryWrapper);
        engineItemDao.dropTables(list);
    }

    public static void main(String[] args) {
        String a = "1234567";
        String b = a.substring(3);
        System.out.println(b);
    }
}
